package com.edu.statisticsservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.commonutils.R;
import com.edu.statisticsservice.client.UcenterMemberClient;
import com.edu.statisticsservice.domain.StatisticsDaily;
import com.edu.statisticsservice.mapper.StatisticsDailyMapper;
import com.edu.statisticsservice.service.StatisticsDailyService;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author ljw
 * @since 2022-09-27
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterMemberClient ucenterMemberClient;
    
    @Override
    public void getDay(String day) {
        //删除已存在的统计对象
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.eq("date_calculated", day);
        baseMapper.delete(dayQueryWrapper);

        //获取用户注册的人数
        R r = ucenterMemberClient.getDate(day);
        Integer count = (Integer) r.getData().get("count");

        //把获取的数据添加到数据 统计分析表里面
        StatisticsDaily statisticsDaily=new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);

        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100,200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(10,200));
        baseMapper.insert(statisticsDaily);
    }

    //根据类型 开始时间 结束时间 查询图标显示
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        //条件
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.select("date_calculated", type);
        wrapper.between("date_calculated", begin, end);

        List<StatisticsDaily> statisticsDailies = baseMapper.selectList(wrapper);

        //因为前端需要的数组结构的json数据
        //需要两个集合 一个是日期的 一个是数量的
        //返回值需要的map结构
        Map<String,Object> map=new HashMap<>();
        List<String> dateList=new ArrayList<>();
        List<Integer> numList=new ArrayList<>();


        //把数据封装
        for (int i = 0; i < statisticsDailies.size(); i++) {
            StatisticsDaily daily = statisticsDailies.get(i);
            dateList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    numList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        map.put("numList", numList);
        map.put("dateList", dateList);
        //返回
        return map;
    }
}
